package com.software.shipperagent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.bookingform.dao.BookingFormTransaction;
import com.software.form.cargobooking.dao.CargoBooking;
import com.software.form.cargobooking.dao.CargoBookingTransaction;
import com.software.form.inquiry.sagent.dao.InquirySagentTransaction;
import com.software.form.inquiryform.dao.InquiryForm;
import com.software.form.inquiryform.dao.InquiryFormTransaction;

public class CargoBookingServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		CargoBookingTransaction cbt = (CargoBookingTransaction) CommonUtils.getBean("cargoBookingTransaction");
		String email = (String) request.getSession().getAttribute("user");
		List<CargoBooking> cargos = cbt.getCargoBySagentEmail(email);
		List list = new ArrayList();
		if(cargos != null){
			for(int i = 0; i < cargos.size(); i++){
				JSONArray array = new JSONArray();
				array.put(cargos.get(i).getNo());
				array.put(cargos.get(i).getShipper());
				array.put(cargos.get(i).getConsignee());
				array.put(cargos.get(i).getCargoName());
				array.put(cargos.get(i).getShipperEmail());
				list.add(array);
			}
			JSONObject o = new JSONObject();
			try {
				o.put("cargos", list);
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
		String action = request.getParameter("action");
		if(action.equals("get")){
			CargoBookingTransaction cbt = (CargoBookingTransaction) CommonUtils.getBean("cargoBookingTransaction");
			try {
				String no = json.getString("no");
				CargoBooking cb = cbt.getCargoByNo(no);
				JSONObject o = new JSONObject();
				o.put("no", cb.getNo());
				o.put("shipper", cb.getShipper());
				o.put("addr", cb.getShipperAddress());
				o.put("email", cb.getShipperEmail());
				o.put("con", cb.getConsignee());
				o.put("conAddr", cb.getConsigneeAddress());
				o.put("departure", cb.getDeparture());
				o.put("dest", cb.getDestination());
				o.put("spec", cb.getSpecialInstructions());
				o.put("cargo", cb.getCargoName());
				o.put("pieces", cb.getPieces());
				o.put("weight", cb.getWeight());
				o.put("m3", cb.getVolume());
				o.put("charges", cb.getCharges());
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("update")){
			CargoBookingTransaction cbt = (CargoBookingTransaction) CommonUtils.getBean("cargoBookingTransaction");
			try {
				String no = json.getString("no");
				if(cbt.updateCargoStatus(no, 1))
					CommonUtils.outputJsonData(1, response);
				else
					CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	
}
